package com.quan.OGCamera;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
//import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

public abstract class MultitouchView extends View {

	//private SparseArray<PointF> mActivePointers;
	private ScaleGestureDetector mScaleDetector;
	private float mScaleFactor = 1.f;
	protected float lastX;
	protected float lastY;
	protected int mainIndex; 
	public MultitouchView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	private void initView(Context context) {
		// Create our ScaleGestureDetector
	    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// Let the ScaleGestureDetector inspect all events.
	    mScaleDetector.onTouchEvent(event);
	    
		// get pointer index from the event object
		int pointerIndex = event.getActionIndex();

		// get pointer ID
		//int pointerId = event.getPointerId(pointerIndex);

		// get masked (not specific to a pointer) action
		int maskedAction = event.getActionMasked();

		// get number of touch
		int pointerCount = event.getPointerCount();
		
		switch (maskedAction) {

		case MotionEvent.ACTION_DOWN:
		case MotionEvent.ACTION_POINTER_DOWN: {
			switch (pointerCount) {
			case 1:
				mainIndex = pointerIndex;
				float x = event.getX(mainIndex);
				float y = event.getY(mainIndex);
				Log.i("index",Integer.toString(mainIndex)+" "+Float.toString(x)+" "+Float.toString(y));
				performSingleTouchActionDown(x,y);break;
			}
			break;
		}
		case MotionEvent.ACTION_MOVE: { // a pointer was moved
			switch (pointerCount) {
			case 1: {
				float x = event.getX(mainIndex);
				float y = event.getY(mainIndex);
				Log.i("index",Integer.toString(mainIndex)+" "+Float.toString(x)+" "+Float.toString(y));
				performSingleTouchActionMove(x,y);
				break;
			}
			case 2: {
				// Handle rotation
			}
			}
		}

		}
		lastX = event.getX(mainIndex);
		lastY = event.getY(mainIndex);
		
		invalidate();

		return true;
	}
	
	private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
	    @Override
	    public boolean onScale(ScaleGestureDetector detector) {
	        mScaleFactor *= detector.getScaleFactor();
	        
	        // Don't let the object get too small or too large.
	        mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
	        performScaling(mScaleFactor);
	        invalidate();
	        return true;
	    }
	}
	//Abstract
	
	//Methods to perform when touches moved for n number of touches
	public abstract void performSingleTouchActionMove(float x,float y);
	
	//This is a separated process with Scaling Detector for 2 finger touched event
	public abstract void performDoubleTouchActionMove(float deltaX1,float deltaY1, 
			float deltaX2, float deltaY2);
	public abstract void performTripleTouchActionMove(float deltaX1,float deltaY1, 
			float deltaX2, float deltaY2, float deltaX3, float deltaY3);
	
	//Methods to perform when touches downed for n number of touches
	public abstract void performSingleTouchActionDown(float x, float y);
	public abstract void performDoubleTouchActionDown(float x1, float y1, float x2, float y2);
	public abstract void performTripleTouchActionDown(float x1, float y1, float x2, float y2, float x3, float y3);	
	
	//Methods to perform when scaling action is happening
	public abstract void performScaling(float scalingFactor);
	
} 
